﻿

using System;
using System.Collections.Generic;
using System.Data;
using BusinessObjects.BussinessDictionary;
using DataObjects.Shared;
using DataObjects.StandartInterfaces.Dictionary;

namespace DataObjects.ADO.SqlServer.Dictionary
{

    /// <summary>
    /// 
    /// </summary>
    public class SqlServerVendorDao : IVendorDao 
    {
        /// <summary>
        /// Takes the specified employ test.
        /// </summary>
        /// <param name="vendor">The employ test.</param>
        /// <returns></returns>
        private static object[] Take(Vendor vendor)
        {
            return new object[]  
            {
                "@VendorID", vendor.VendorId,
                "@Code", vendor.Code,
                "@Description", vendor.Description,
                "@Address", vendor.Address,
                "@PhoneNumber", vendor.PhoneNumber,
                "@ContactName", vendor.ContactName,
                "@UnitId", vendor.UnitId,
                "@Name", vendor.Name,
                "@Fax", vendor.Fax,
                "@IsActive", vendor.IsActive
            };
        }
        /// <summary>
        /// The make
        /// </summary>
        private static readonly Func<IDataReader, Vendor> Make = reader =>
          new Vendor
          {
              VendorId = reader["VendorID"].AsId(),
              Code = reader["Code"].AsString(),
              Description = reader["Description"].AsString(),
              Address = reader["Address"].AsString(),
              PhoneNumber = reader["PhoneNumber"].AsString(),
              ContactName = reader["ContactName"].AsString(),
              UnitId = reader["UnitID"].AsInt(),
              Name = reader["Name"].AsString(),
              Fax = reader["Fax"].AsString(),
              IsActive = reader["IsActive"].AsBool()
          };

        /// <summary>
        /// Gets the paged.
        /// </summary>
        /// <param name="unitId">The unit identifier.</param>
        /// <param name="currentPage">The current page.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public List<Vendor> GetPaged(int unitId, int currentPage)
        {
            const string sql = @"uspGetPaged_Vendor";
            object[] parms = { "@WhereClause", null, "@OrderBy", null, "@PageIndex", currentPage - 1, "@UnitID", unitId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// </summary>
        /// <param name="unitId"></param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public int GetPageSize(int unitId)
        {
            const string sql = @"uspGetPageSize_Vendor";
            object[] parms = { "@WhereClause", null, "@UnitID", unitId };
            return Db.GetCount(sql, CommandType.StoredProcedure, parms);
        }

       

       

        /// <summary>
        /// Inserts the vendor.
        /// </summary>
        /// <param name="vendor">The vendor.</param>
        /// <returns></returns>
        public string InsertVendor(Vendor vendor)
        {
            const string sql = "uspInsert_Vendor";
            return Db.Insert(sql, CommandType.StoredProcedure, Take(vendor));
        }

        /// <summary>
        /// Updates the vendor.
        /// </summary>
        /// <param name="vendor">The vendor.</param>
        /// <returns></returns>
        public string UpdateVendor(Vendor vendor)
        {
            const string sql = "uspUpdate_Vendor";
            return Db.Update(sql, CommandType.StoredProcedure, Take(vendor));
        }

        /// <summary>
        /// Deletes the vendor.
        /// </summary>
        /// <param name="vendor">The vendor.</param>
        /// <returns></returns>
        public string DeleteVendor(Vendor vendor)
        {
            const string sql = @"uspDelete_Vendor";
            object[] parms = { "@VendorID", vendor.VendorId ,
                             "@UnitID", vendor.UnitId};
            return Db.Delete(sql, CommandType.StoredProcedure, parms);
        }





        public Vendor GetVendor(int vendorId)
        {
            const string sql = @"uspGet_Vendor_ByID";
            object[] parms = { "@VendorID", vendorId };
            return Db.Read(sql, CommandType.StoredProcedure,Make, parms);
        }


        public List<Vendor> GetVendorByUnit(int unitId)
        {
            const string sql = @"uspGet_Vendor_ByUnitID";
            object[] parms = { "@UnitID", unitId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }
    }
}
